﻿using MiniFox.Components;
using MiniFox.Configurations;
using Wechat.Corp.Configurations;
using Wechat.Corp.Models;

namespace Wechat.Corp
{
    /// <summary>
    /// 
    /// </summary>
    [AutoRegister, SpareComponent(typeof(DefaultWechatCorpConfigProvider))]
    public abstract class WechatCorpConfigProvider
    {
        /// <summary>
        /// 
        /// </summary>
        protected WechatCorpConfigProvider()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        abstract public IWechatCorpConfiguration GetSecureConfig();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        abstract public IWechatAgent GetAgentSetting(string name);

    }

    internal class DefaultWechatCorpConfigProvider : WechatCorpConfigProvider
    {
        IWechatCorpConfiguration _config;
        public override IWechatCorpConfiguration GetSecureConfig()
        {
            if (_config == null)
            {
                var config = ConfigurationFactory.GetConfig<WechatCorpSecurityConfiguration>();
                _config = new WechatCorpSecure
                {
                    CorpID = config.CorpID,
                    Agents = config.Agents,
                    Default = config.Default
                };
            }
            return _config;
        }
        public override IWechatAgent GetAgentSetting(string name)
        {
            var config = this.GetSecureConfig();
            name = (string.IsNullOrWhiteSpace(name)) ? config.Default : name;
            return config.Agents[name];
        }

    }
}
